package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

/**
 *  HljNewsAnalyse  黑龙江新闻网新闻解析类
 *  xiaonie 2012-11-22
 */
public class HljNewsAnalyse implements AnalyseNews{
	private final String[] regexs={ "http://www.hljnews.cn/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*[.]htm",
			
			"http://.*.hljnews.cn/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9].*.shtml"};

	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String str_encode=urlMeta.getEncoding();
		String url=urlMeta.getUrl();
		
		if (!isDetailPage(url)) {
		}
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		if (htmltxt != null && htmltxt != "") {
			news.setUrl(url);
			title=doc.select("td.title_24hei").text();
			if(title == null || title.trim().length() == 0){
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("td#details_contents"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url, doc.select("td.z14hei"));
			}
			dateStr=doc.select("div#details_s,div.z12heititle").text();
			if(StringUtils.isBlank(dateStr)){
				dateStr=url;
			}
			
			date=DateUtils.matchDate(dateStr);
			if (date == null) {
				date=JSoupUtils.matchDate(doc, "来源：");
			}
			news.setContent(content);
			news.setTitle(title);
			news.setClickNum(null);
			Integer commentNum=0;
			try {
				commentNum=Integer.parseInt(doc.select("span#commentCount").text());
			    news.setCommentNum(commentNum);
			} catch (Exception e) {
				// TODO: handle exception
			}
			news.setUpdateUrl(url);
			news.setDate(date);
			news.setType(1);
			String author=doc.select("div#details_s span").text();
			if(author!=null && author.indexOf("来源：")!=-1){
				author=StringUtils.substringAfter(author, "来源：");
			}
			if(StringUtils.isBlank(author)){
				author=doc.select("span.z12heititle").text();
				if(author!=null && author.indexOf("来源：")!=-1){
					author=StringUtils.substringAfter(author, "来源：");
					author=author.replace("黑龙江新闻网—", "");
				}
			}
			news.setAuthor(author);
		}
		return news;
	}

	public static void main(String[] args) {
		HljNewsAnalyse a=new HljNewsAnalyse();
		String url="http://hlj.hljnews.cn/system/2014/01/13/010043590.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(a.isDetailPage(url));
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			UrlMeta meta1=CrawlHTML.responseToURL(meta.getUrl());
			if (meta1.getHtml() == null) {
				return null;
			}
			String htmltxt=meta1.getHtml();
			Document doc=Jsoup.parse(htmltxt);
			Integer commentNum=0;
			try {
				commentNum=Integer.parseInt(doc.select("span#commentCount").text());
			} catch (Exception e) {
				// TODO: handle exception
			}
			meta.setCommentNum(commentNum);
			return meta;
		}
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
